# Copyright 2023 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for audio_frontend.python.utils.freq_to_mel."""
import unittest
from tflite_micro.python.tflite_micro.signal.utils import freq_to_mel_wrapper

# This table was generated by C the implementation of Freq2Mel() in Speech
# Micro. The purpose of this test is to catch changes in clang/GCC's C
# implementation and update our unit tests accordingly.
# The table consists of mapping of frequencies in the range 125 Hz - 3850 Hz,
# which are typically used in Mel filter banks.
truth_table = [[125.00000000000000000000, 185.16954040527343750000],
               [156.25000000000000000000, 227.07028198242187500000],
               [187.50000000000000000000, 267.46884155273437500000],
               [218.75000000000000000000, 306.46929931640625000000],
               [250.00000000000000000000, 344.16513061523437500000],
               [281.25000000000000000000, 380.64077758789062500000],
               [312.50000000000000000000, 415.97283935546875000000],
               [343.75000000000000000000, 450.23080444335937500000],
               [375.00000000000000000000, 483.47805786132812500000],
               [406.25000000000000000000, 515.77252197265625000000],
               [437.50000000000000000000, 547.16729736328125000000],
               [468.75000000000000000000, 577.71118164062500000000],
               [500.00000000000000000000, 607.44909667968750000000],
               [531.25000000000000000000, 636.42242431640625000000],
               [562.50000000000000000000, 664.66949462890625000000],
               [593.75000000000000000000, 692.22583007812500000000],
               [625.00000000000000000000, 719.12451171875000000000],
               [656.25000000000000000000, 745.39605712890625000000],
               [687.50000000000000000000, 771.06921386718750000000],
               [718.75000000000000000000, 796.17047119140625000000],
               [750.00000000000000000000, 820.72473144531250000000],
               [781.25000000000000000000, 844.75561523437500000000],
               [812.50000000000000000000, 868.28460693359375000000],
               [843.75000000000000000000, 891.33245849609375000000],
               [875.00000000000000000000, 913.91833496093750000000],
               [906.25000000000000000000, 936.06048583984375000000],
               [937.50000000000000000000, 957.77606201171875000000],
               [968.75000000000000000000, 979.08093261718750000000],
               [1000.00000000000000000000, 999.99072265625000000000],
               [1031.25000000000000000000, 1020.51953125000000000000],
               [1062.50000000000000000000, 1040.68103027343750000000],
               [1093.75000000000000000000, 1060.48828125000000000000],
               [1125.00000000000000000000, 1079.95336914062500000000],
               [1156.25000000000000000000, 1099.08789062500000000000],
               [1187.50000000000000000000, 1117.90295410156250000000],
               [1218.75000000000000000000, 1136.40930175781250000000],
               [1250.00000000000000000000, 1154.61633300781250000000],
               [1281.25000000000000000000, 1172.53405761718750000000],
               [1312.50000000000000000000, 1190.17138671875000000000],
               [1343.75000000000000000000, 1207.53686523437500000000],
               [1375.00000000000000000000, 1224.63879394531250000000],
               [1406.25000000000000000000, 1241.48522949218750000000],
               [1437.50000000000000000000, 1258.08349609375000000000],
               [1468.75000000000000000000, 1274.44091796875000000000],
               [1500.00000000000000000000, 1290.56408691406250000000],
               [1531.25000000000000000000, 1306.45996093750000000000],
               [1562.50000000000000000000, 1322.13476562500000000000],
               [1593.75000000000000000000, 1337.59448242187500000000],
               [1625.00000000000000000000, 1352.84509277343750000000],
               [1656.25000000000000000000, 1367.89208984375000000000],
               [1687.50000000000000000000, 1382.74084472656250000000],
               [1718.75000000000000000000, 1397.39648437500000000000],
               [1750.00000000000000000000, 1411.86389160156250000000],
               [1781.25000000000000000000, 1426.14807128906250000000],
               [1812.50000000000000000000, 1440.25317382812500000000],
               [1843.75000000000000000000, 1454.18420410156250000000],
               [1875.00000000000000000000, 1467.94506835937500000000],
               [1906.25000000000000000000, 1481.53991699218750000000],
               [1937.50000000000000000000, 1494.97265625000000000000],
               [1968.75000000000000000000, 1508.24731445312500000000],
               [2000.00000000000000000000, 1521.36743164062500000000],
               [2031.25000000000000000000, 1534.33654785156250000000],
               [2062.50000000000000000000, 1547.15795898437500000000],
               [2093.75000000000000000000, 1559.83532714843750000000],
               [2125.00000000000000000000, 1572.37158203125000000000],
               [2156.25000000000000000000, 1584.77001953125000000000],
               [2187.50000000000000000000, 1597.03332519531250000000],
               [2218.75000000000000000000, 1609.16491699218750000000],
               [2250.00000000000000000000, 1621.16711425781250000000],
               [2281.25000000000000000000, 1633.04284667968750000000],
               [2312.50000000000000000000, 1644.79479980468750000000],
               [2343.75000000000000000000, 1656.42553710937500000000],
               [2375.00000000000000000000, 1667.93725585937500000000],
               [2406.25000000000000000000, 1679.33276367187500000000],
               [2437.50000000000000000000, 1690.61401367187500000000],
               [2468.75000000000000000000, 1701.78369140625000000000],
               [2500.00000000000000000000, 1712.84350585937500000000],
               [2531.25000000000000000000, 1723.79614257812500000000],
               [2562.50000000000000000000, 1734.64318847656250000000],
               [2593.75000000000000000000, 1745.38671875000000000000],
               [2625.00000000000000000000, 1756.02893066406250000000],
               [2656.25000000000000000000, 1766.57165527343750000000],
               [2687.50000000000000000000, 1777.01647949218750000000],
               [2718.75000000000000000000, 1787.36547851562500000000],
               [2750.00000000000000000000, 1797.62036132812500000000],
               [2781.25000000000000000000, 1807.78283691406250000000],
               [2781.25000000000000000000, 1807.78283691406250000000],
               [2812.50000000000000000000, 1817.85437011718750000000],
               [2843.75000000000000000000, 1827.83666992187500000000],
               [2875.00000000000000000000, 1837.73144531250000000000],
               [2906.25000000000000000000, 1847.54003906250000000000],
               [2937.50000000000000000000, 1857.26403808593750000000],
               [2968.75000000000000000000, 1866.90466308593750000000],
               [3000.00000000000000000000, 1876.46374511718750000000],
               [3031.25000000000000000000, 1885.94238281250000000000],
               [3062.50000000000000000000, 1895.34204101562500000000],
               [3093.75000000000000000000, 1904.66369628906250000000],
               [3125.00000000000000000000, 1913.90905761718750000000],
               [3156.25000000000000000000, 1923.07922363281250000000],
               [3187.50000000000000000000, 1932.17517089843750000000],
               [3218.75000000000000000000, 1941.19848632812500000000],
               [3250.00000000000000000000, 1950.15014648437500000000],
               [3281.25000000000000000000, 1959.03112792968750000000],
               [3312.50000000000000000000, 1967.84277343750000000000],
               [3343.75000000000000000000, 1976.58605957031250000000],
               [3375.00000000000000000000, 1985.26196289062500000000],
               [3406.25000000000000000000, 1993.87158203125000000000],
               [3437.50000000000000000000, 2002.41601562500000000000],
               [3468.75000000000000000000, 2010.89611816406250000000],
               [3500.00000000000000000000, 2019.31298828125000000000],
               [3531.25000000000000000000, 2027.66723632812500000000],
               [3562.50000000000000000000, 2035.96020507812500000000],
               [3593.75000000000000000000, 2044.19250488281250000000],
               [3625.00000000000000000000, 2052.36523437500000000000],
               [3656.25000000000000000000, 2060.47900390625000000000],
               [3687.50000000000000000000, 2068.53466796875000000000],
               [3718.75000000000000000000, 2076.53344726562500000000],
               [3750.00000000000000000000, 2084.47558593750000000000],
               [3781.25000000000000000000, 2092.36230468750000000000],
               [3812.50000000000000000000, 2100.19409179687500000000]]


class Freq2MelTest(unittest.TestCase):

  def testFreq2Mel(self):
    for entry in truth_table:
      mel = freq_to_mel_wrapper.freq_to_mel(entry[0])
      self.assertLess(abs(mel - float(entry[1])), 0.00025)


if __name__ == '__main__':
  unittest.main()
